package com.gome.ss.core.plugin;

import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.gome.ss.core.download.SpiderRequest;
import com.gome.ss.core.download.SpiderRequestInterface;
import com.gome.ss.core.download.StartRequest;
import com.gome.ss.core.queue.QueueCache;
import com.gome.ss.core.schedule.ScheduJobConfig;
import com.gome.ss.core.schedule.ScheduleCore;
import com.gome.ss.core.schedule.ScheduleManager;

public class PluginTaskBase implements Job {
	private static Logger logger = Logger.getLogger(PluginTaskBase.class);
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		
		PluginTaskProcess pluginTaskProcess = (PluginTaskProcess) arg0.getJobDetail().getJobDataMap().get("pluginTaskProcess");
		String taskID = pluginTaskProcess.getTaskID();
		
		SpiderPluginHandle handle = (SpiderPluginHandle) arg0.getJobDetail().getJobDataMap().get("handle");
		if (handle.isProcessing()) {
			logger.info("Plugin is processing : " + pluginTaskProcess.getJobName() + " | " + pluginTaskProcess.getTaskID());
			return;
		}
		
		String startMethod = (String) arg0.getJobDetail().getJobDataMap().get("startMethod");
		ScheduJobConfig jobConfig = (ScheduJobConfig) arg0.getJobDetail().getJobDataMap().get("scheduJobConfig");
		jobConfig.updateTaskStartStatus(handle.getPluginName(), handle.getPluginQualifiedName(), jobConfig.getTaskID());
		
		boolean incremental = (Boolean) arg0.getJobDetail().getJobDataMap().get("incremental");
		
		handle.start(incremental);
		logger.info("Start plugin task : " + pluginTaskProcess.getJobName() + " | " + pluginTaskProcess.getTaskID());
		
		ScheduleCore sc = ScheduleManager.getInstance().getScheduleCore(handle.getPluginQualifiedName());
		
		if (startMethod.equals(PluginManager.START_REQUEST_METHOD_FUNCTION)) {
			List<StartRequest> sreqs = handle.getStartRequest();
			while (sreqs != null && handle.isProcessing()) {
				for (StartRequest sreq : sreqs) {
					String qualifiedString = sreq.qualified;
					
					if (qualifiedString.equals("")) {
						qualifiedString = handle.getPluginQualified();
					}
					
					SpiderRequestInterface request = null;
					if (sreq.isStatic)
						request = SpiderRequest.createStatic(sreq.link, sreq.groupName, handle.getPluginName(), qualifiedString, QueueCache.defaultPriority, sreq.retryCount);
					else
						request = SpiderRequest.create(sreq.link, sreq.groupName, handle.getPluginName(), qualifiedString, QueueCache.defaultPriority, sreq.retryCount);
					
					request.setSourceTaskID(taskID);
					
					sc.pushRequestToQueue(request, request.getPriority());
				}
				sreqs = handle.getStartRequest();
			}
		} else if (startMethod.equals(PluginManager.START_REQUEST_METHOD_XML) ||
					startMethod.equals(PluginManager.START_REQUEST_METHOD_CONFIG)) {
			List<SpiderRequestInterface> requests = (List<SpiderRequestInterface>) arg0.getJobDetail().getJobDataMap().get("requests");
			for (SpiderRequestInterface request : requests) {
				request.setSourceTaskID(taskID);
				sc.pushRequestToQueue(request, request.getPriority(), false);
			}
		}
		
		handle.setStarted();
	}
}
